package com.fengmoxi.ctv.manage;

import android.util.Log;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;


public class HdmiInCheckManage {
    private static final String TAG = HdmiInCheckManage.class.getSimpleName();

    public Boolean getState() {
        try {
            FileInputStream fis = new FileInputStream("/sys/devices/platform/hdmi-in-check/keys_state");
            byte[] buf = new byte[2048];
            if (fis.read(buf) != -1) {
                String str = new String(buf, StandardCharsets.UTF_8);
                String[] values = str.trim().split("\n");
                return Objects.equals(values[0], "1");
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "found error: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IO error: " + e.getMessage());
        } catch (SecurityException e) {
            Log.e(TAG, "Security error: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage());
        }
        return false;
    }
}
